﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace HMS
{
    public partial class ucDanhsachbenhnhan : UserControl
    {
        private SqlConnection con = new SqlConnection(FRM_Manhinhchinh.sConn);
        private SqlDataAdapter dataAp;
        private int iIndex;
        private DataTable dataTable;
        public static string sGetID;
        public static bool bUpdate, bClick;

        public ucDanhsachbenhnhan()
        {
            InitializeComponent();
            load_data();
        }// end

        // Load dữ liệu lên gridDanhsach
        public void load_data()
        {
            string sSQL = "select patient_id, patient_fname, patient_lname, "
                 + "patient_gender, patient_address, patient_phonenumber, patient_email, c.city_name," +
                 " d.district_name, patient_year from tblpatient p, tblcity c, tbldistrict d "
                 + "where p.city_id = c.city_id and p.district_id = d.district_id";
            GetDataTable(sSQL);
            gridDanhsach.DataSource = dataTable;
        }// end load_data

        public DataTable GetDataTable(string sql)
        {
            // Tạo dataApdapter, thực hiện câu lệnh query
            dataAp = new SqlDataAdapter(sql, con);
            // Đổ dữ liệu vào DataTable
            dataTable = new DataTable();
            dataAp.Fill(dataTable);
            return dataTable;
        }// end GetDataTable

        // Event click cho nút Tạo hồ sơ bệnh nhân
        private void btnTaohosobenhnhan_Click(object sender, EventArgs e)
        {
            bClick = false;
            bUpdate = false;
            FRM_Taohosobenhnhan frm = new FRM_Taohosobenhnhan();
            DialogResult result = frm.ShowDialog();
            if (result == DialogResult.OK)
                load_data();
        }// end btnTaohosobenhnhan_Click

        // Event click cho nút Tạo hồ sơ bệnh nhân
        private void btnCapnhatbenhnhan_Click(object sender, EventArgs e)
        {
            bClick = false;
            bUpdate = true;
            try
            {
                if (sGetID == "") return;
                else
                {
                    FRM_Taohosobenhnhan frm = new FRM_Taohosobenhnhan();
                    DialogResult result = frm.ShowDialog();
                    if (result == DialogResult.OK)
                        load_data();
                }// end else
            }// end try
            catch { return; }
        }// btnCapnhatbenhnhan_Click

        // Event click cho nút Tạo phiếu khám
        private void btnTaophieukham_Click(object sender, EventArgs e)
        {
            bClick = true;
            FRM_Taophieukham frm = new FRM_Taophieukham();
            frm.ShowDialog();
        }// end btnTaophieukham_Click

        // Event click cho gridDanhsach
        private void gridDanhsach_Click(object sender, EventArgs e)
        {
            bClick = true;
            btnCapnhatbenhnhan.Enabled = true;
            btnTaophieukham.Enabled = true;
            btnXemphieukham.Enabled = true;
            try
            {
                iIndex = gridView1.FocusedRowHandle;
                sGetID = gridView1.GetRowCellValue(iIndex, "patient_id").ToString();
            }// end try
            catch
            {
                btnTaophieukham.Enabled = false;
                btnCapnhatbenhnhan.Enabled = false;
                btnXemphieukham.Enabled = false;
            }// end catch
        }// end gridDanhsach_Click

        // Event click cho nút Xem phiếu khám
        private void btnXemphieukham_Click(object sender, EventArgs e)
        {
            FRM_Xemphieukham frm = new FRM_Xemphieukham();
            frm.sMaBN = sGetID;
            frm.ShowDialog();
        }// end btnXemphieukham_Click

        // Event double click cho gridDanhsach
        private void gridDanhsach_DoubleClick(object sender, EventArgs e)
        {
            btnCapnhatbenhnhan_Click(null, null);
        }

        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column==sothutu)
            {
                e.DisplayText = (e.RowHandle + 1).ToString();
            }
        }// end gridDanhsach_DoubleClick

    }// end
}// end namespace